<template>
  <div class="search-section">
    <div class="search-box">
      <Search :size="20" class="search-icon" />
      <input
        id="contact-search"
        v-model="searchValue"
        type="text"
        :placeholder="placeholder"
        class="search-input"
        role="searchbox"
        :aria-label="placeholder"
        @input="handleInput"
        @keydown.enter="handleEnter"
      />
    </div>
    <ActionButton
      :icon="UserPlus"
      variant="primary"
      text="添加好友"
      show-text
      tooltip="添加新的好友"
      @click="handleAddContact"
    />
  </div>
</template>

<script setup lang="ts">
import { ref, watch, onUnmounted } from 'vue'
import { Search, UserPlus } from 'lucide-vue-next'
import ActionButton from './ContactItem/ActionButton.vue'

interface Props {
  modelValue?: string
  placeholder?: string
}

interface Emits {
  (e: 'update:modelValue', value: string): void
  (e: 'search', value: string): void
  (e: 'add-contact'): void
  (e: 'enter', value: string): void
}

const props = withDefaults(defineProps<Props>(), {
  modelValue: '',
  placeholder: '搜索联系人...'
})

const emit = defineEmits<Emits>()

const searchValue = ref(props.modelValue)

// 监听外部值变化
watch(
  () => props.modelValue,
  (newValue) => {
    searchValue.value = newValue
  }
)

// 监听内部值变化 - 添加防抖优化
let searchTimeout: ReturnType<typeof setTimeout> | null = null
watch(searchValue, (newValue) => {
  emit('update:modelValue', newValue)

  // 防抖处理搜索
  if (searchTimeout) {
    clearTimeout(searchTimeout)
  }
  searchTimeout = setTimeout(() => {
    emit('search', newValue)
  }, 300)
})

const handleInput = (event: Event): void => {
  const target = event.target as HTMLInputElement
  searchValue.value = target.value
}

const handleEnter = (): void => {
  emit('enter', searchValue.value)
}

const handleAddContact = (): void => {
  emit('add-contact')
}

// 组件销毁时清理定时器
onUnmounted(() => {
  if (searchTimeout) {
    clearTimeout(searchTimeout)
  }
})
</script>

<style scoped>
.search-section {
  display: flex;
  gap: 16px;
  margin-bottom: 24px;
  align-items: center;
}

.search-box {
  flex: 1;
  position: relative;
  display: flex;
  align-items: center;
}

.search-icon {
  position: absolute;
  left: 12px;
  color: var(--text-color-secondary);
  z-index: 1;
  pointer-events: none;
}

.search-input {
  width: 100%;
  padding: 12px 12px 12px 44px;
  border: 1px solid var(--border-color-light);
  border-radius: 8px;
  background-color: var(--background-color-secondary);
  color: var(--text-color-primary);
  font-size: 14px;
  outline: none;
  transition: border-color 0.3s ease;
}

.search-input:focus {
  border-color: var(--primary-color);
}

.search-input::placeholder {
  color: var(--text-color-secondary);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .search-section {
    flex-direction: column;
    gap: 12px;
  }
}

@media (max-width: 480px) {
  .search-section {
    gap: 8px;
  }

  .search-input {
    padding: 10px 10px 10px 36px;
    font-size: 16px; /* 防止iOS缩放 */
  }

  .search-icon {
    left: 10px;
  }
}
</style>
